package ink.xiaobaibai.filter;

import ink.xiaobaibai.handler.LoginFailedHandler;
import ink.xiaobaibai.handler.LoginSuccessHandler;
import ink.xiaobaibai.token.MyUsernamePasswordAuthenticationToken;
import org.springframework.lang.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 账号密码登录过滤器
 * @author: 小白白
 * @create: 2021-05-11
 **/


public class MyUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    private String usernameParameter = "username";
    private String passwordParameter = "password";
    private String verificationCodeParameter = "verificationCode";
    private String uuidParameter = "uuid";
    private static String loginUrl = "/login";

    public MyUsernamePasswordAuthenticationFilter() {
        super(new AntPathRequestMatcher(loginUrl, "POST"));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        String username = this.obtainUsername(request);
        String password = this.obtainPassword(request);
        String uuid = this.obtainUUID(request);
        String verificationCode = this.obtainVerificationCode(request);
        if (username == null) {
            username = "";
        }

        if (password == null) {
            password = "";
        }

        if (verificationCode == null) {
            verificationCode = "";
        }
        if (uuid != null && uuid.length() > 32) {
            uuid = null;
        }

        username = username.trim();
        MyUsernamePasswordAuthenticationToken authRequest =
                new MyUsernamePasswordAuthenticationToken(username, password, uuid, verificationCode);
        this.setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    /**
     * 验证失败处理器
     */
    @Override
    public void setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
        super.setAuthenticationFailureHandler(new LoginFailedHandler());
    }

    /**
     * 设置成功处理器
     */
    @Override
    public void setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        super.setAuthenticationSuccessHandler(new LoginSuccessHandler());
    }


    protected void setDetails(HttpServletRequest request, MyUsernamePasswordAuthenticationToken authRequest) {
        authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
    }

    @Nullable
    protected String obtainUUID(HttpServletRequest request) {
        return request.getParameter(this.uuidParameter);
    }

    @Nullable
    protected String obtainPassword(HttpServletRequest request) {
        return request.getParameter(this.passwordParameter);
    }

    @Nullable
    protected String obtainUsername(HttpServletRequest request) {
        return request.getParameter(this.usernameParameter);
    }

    @Nullable
    protected String obtainVerificationCode(HttpServletRequest request) {
        return request.getParameter(this.verificationCodeParameter);
    }

}
